package cn.tedu.sp02.controller;

import cn.tedu.sp01.pojo.Item;
import cn.tedu.sp01.service.ItemService;
import cn.tedu.sp01.web.util.JsonResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Random;

@RestController//@Controller+@Responsity
@Slf4j//日志
public class ItemController {

    @Autowired//按类型查找注入
    private ItemService itemService;

    @Value("${server.port}")
    private int port;

    @GetMapping("/{orderId}")
    public JsonResult<List<Item>> getItems(@PathVariable String orderId) throws InterruptedException{
        log.info("server.port="+port+",orderId="+orderId);
        List<Item> items = itemService.getItems(orderId);
        if(Math.random() < 0.9){
            int t = new Random().nextInt(5000);
            System.out.println("延迟: "+t);
            Thread.sleep(t);
        }
        return JsonResult.ok(items).msg("port="+port);
    }

    @PostMapping("/decreaseNumber")
    public JsonResult decreaseNumber(@RequestBody List<Item> item){
        itemService.decreaseNumber(item);
        return JsonResult.build().code(200).msg("减少库存成功");
    }
    @GetMapping("/favicon.ico")
    public void ico(){

    }
}
